package b3.widget.basic
{
	import b3.net.*;
	import b3.widget.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.text.*;
	import flash.ui.Mouse;
	import flash.utils.*;

	public class BasicIcon extends Sprite
	{
		private var iconImage:Sprite;
		private var oldPosX:Number;
		private var oldPosY:Number;
		//0, 上方；1，下方
		private var tipPosType:int;
		private var tipText:String;
		private var oldWidth:Number;
		private var oldHeight:Number;
		private var mouseOverTipFlag:Boolean;
		private var shineFlag:Boolean;
		
		public function BasicIcon(IconImage:Class, tipText:String = null, tipPosType:int = 0, textEmbeddedFlag:Boolean = false, iconBackgroundFlag:Boolean = false, textColor:int = 0x000000)
		{
			super();
			this.tipText = tipText;
			this.tipPosType = tipPosType;
			mouseOverTipFlag = true;
			if (iconBackgroundFlag)
			{
				var IconBackground:Class = ResourceHelper.getInstance().getClassByName("IconBackground");
				iconImage = new IconBackground();
				addChild(iconImage);
				//图标图片
				var icon:Sprite = new IconImage();
				icon.width = 35;
				icon.height = 35;
				icon.x = (iconImage.width - icon.width) / 2;
				icon.y = (iconImage.height - icon.height) / 2;
				icon.mouseEnabled = false;
				iconImage.addChild(icon);
				iconImage.mouseChildren = false;
				addChild(iconImage);
			}
			else
			{
				//图标图片
				iconImage = new IconImage();
				iconImage.mouseChildren = false;
				addChild(iconImage);
			}
			//文字嵌入到图片下部
			if (tipText != null && textEmbeddedFlag)
			{
				var embeddedText:TextField = new TextField();
				embeddedText.selectable = false;
	        	embeddedText.autoSize = TextFieldAutoSize.CENTER;
	        	if (tipText.length < 5)
					embeddedText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x0088FF);
				else
	        		embeddedText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x0088FF);
				embeddedText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
				embeddedText.text = tipText;
				embeddedText.x = (iconImage.width - embeddedText.width) / 2;
				if (tipPosType == 0)
					embeddedText.y = -embeddedText.height;
				else
					embeddedText.y = iconImage.height - 5;
				iconImage.addChild(embeddedText);
			}
			oldPosX = iconImage.x;
			oldPosY = iconImage.y;
			oldWidth = iconImage.width;
			oldHeight = iconImage.height;
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
			if (shineFlag)
				this.addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
		}

		private function onRemovedFromStage(event:Event):void
		{
			if (shineFlag)
				this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		public function disableMouseOverTip():void
		{
			mouseOverTipFlag = false;
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			if (tipText != null && mouseOverTipFlag)
			{
				var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
				var tip:BasicMessage = new BasicMessage(tipText); 
				if (tipPosType == 0)
				{
					tip.x = globalPoint.x + (iconImage.width - tip.width) / 2;
					tip.y = globalPoint.y - tip.height - 10;
				}
				else
				{
					tip.x = globalPoint.x + (iconImage.width - tip.width) / 2;
					tip.y = globalPoint.y + iconImage.height + 10;
				}
				WidgetManager.getInstance().popOutMouseOverMessage(tip);
			}
			var scale:Number = 1.2;
			TweenLite.to(iconImage, 0.2, {scaleX:scale, scaleY:scale, x:oldPosX - (scale - 1) * oldWidth / 2, y:oldPosY - (scale - 1) * oldHeight / 2});
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			WidgetManager.getInstance().closeMouseOverMessage();
			TweenLite.to(iconImage, 0.2, {scaleX:1, scaleY:1, x:oldPosX, y:oldPosY});
		}
		
		public function startShine():void
		{
			if (!shineFlag)
			{
				this.shineFlag = true;
				this.addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
			}
		}
		
		public function stopShine():void
		{
			if (shineFlag)
			{
				this.shineFlag = false;
				this.filters = null;
				this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
		}
		
		private function onEnterFrame(event:Event):void
		{
			var shineFilterAlpha:Number = Math.sin(getTimer() / 150);
			var gradientGlow:GradientGlowFilter = new GradientGlowFilter();
			gradientGlow.distance = 0;
			gradientGlow.angle = 45;
			gradientGlow.colors = [0xFFFF00, 0xFFFF00];
			gradientGlow.alphas = [0, shineFilterAlpha];
			gradientGlow.ratios = [0, 255];
			gradientGlow.blurX = 15;
			gradientGlow.blurY = 15;
			gradientGlow.strength = 3;
			gradientGlow.quality = BitmapFilterQuality.HIGH;
			gradientGlow.type = BitmapFilterType.OUTER;
			this.filters = [gradientGlow];
		}
	}
}